<template>
  <view class="page_diy_view page_student_information_view" :style="{ paddingTop: vuex_custom_bar_height + 'px' }">
    <tn-nav-bar>学生信息详情</tn-nav-bar>
    <view id="page_diy_view" class="page-from">
      <uni-forms :modelValue="form" v-if="is_view()" labelWidth="70px">

        <uni-forms-item v-if="$check_field('get','student_name') || ($check_field('add','student_name') || $check_field('set','student_name'))" label="学生姓名" name="student_name">
                      <uni-easyinput type="text" v-model="form['student_name']" v-if="(form['student_information_id'] && $check_field('set','student_name')) || (!form['student_information_id'] && $check_field('add','student_name'))" :disabled="disabledObj['student_name_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','student_name')">
            {{ form['student_name'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','student_account') || ($check_field('add','student_account') || $check_field('set','student_account'))" label="学生账户" name="student_account">
              <uni-data-select
              id="form_student_account"
              v-model="form['student_account']"
              :localdata="list_user_student_account"
              :clear="!disabledObj['student_account_isDisabled']"
              :disabled="disabledObj['student_account_isDisabled']"
              v-if="(form['student_information_id'] && $check_field('set', 'student_account')) || (!form['student_information_id'] && $check_field('add', 'student_account'))"
			   style="width: 100%;"
          ></uni-data-select>
          <uni-data-select
              v-model="form['student_account']"
              :localdata="list_user_student_account"
              :clear="false"
              :disabled="true"
              v-else-if="$check_field('get', 'student_account')"
              id="student_account"
			  style="width: 100%;"
          ></uni-data-select>
            </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','student_gender') || ($check_field('add','student_gender') || $check_field('set','student_gender'))" label="学生性别" name="student_gender">
              <uni-data-select
              v-model="form.student_gender"
              :localdata="list_student_gender"
              :clear="!disabledObj['student_gender_isDisabled']"
              :disabled="disabledObj['student_gender_isDisabled']"
              v-if="(form['student_information_id'] && $check_field('set', 'student_gender')) || (!form['student_information_id'] && $check_field('add', 'student_gender'))"
			  style="width: 100%;"
          ></uni-data-select>
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get', 'student_gender')">
            {{ form['student_gender'] }}
          </text>
            </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','student_id') || ($check_field('add','student_id') || $check_field('set','student_id'))" label="学生学号" name="student_id">
                      <uni-easyinput type="text" v-model="form['student_id']" v-if="(form['student_information_id'] && $check_field('set','student_id')) || (!form['student_information_id'] && $check_field('add','student_id'))" :disabled="disabledObj['student_id_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','student_id')">
            {{ form['student_id'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','class_director') || ($check_field('add','class_director') || $check_field('set','class_director'))" label="班级主任" name="class_director">
              <uni-data-select
              id="form_class_director"
              v-model="form['class_director']"
              :localdata="list_user_class_director"
              :clear="!disabledObj['class_director_isDisabled']"
              :disabled="disabledObj['class_director_isDisabled']"
              v-if="(form['student_information_id'] && $check_field('set', 'class_director')) || (!form['student_information_id'] && $check_field('add', 'class_director'))"
			   style="width: 100%;"
          ></uni-data-select>
          <uni-data-select
              v-model="form['class_director']"
              :localdata="list_user_class_director"
              :clear="false"
              :disabled="true"
              v-else-if="$check_field('get', 'class_director')"
              id="class_director"
			  style="width: 100%;"
          ></uni-data-select>
            </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','students_hometown') || ($check_field('add','students_hometown') || $check_field('set','students_hometown'))" label="学生籍贯" name="students_hometown">
                      <uni-easyinput type="text" v-model="form['students_hometown']" v-if="(form['student_information_id'] && $check_field('set','students_hometown')) || (!form['student_information_id'] && $check_field('add','students_hometown'))" :disabled="disabledObj['students_hometown_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','students_hometown')">
            {{ form['students_hometown'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','student_college') || ($check_field('add','student_college') || $check_field('set','student_college'))" label="学生学院" name="student_college">
                      <uni-easyinput type="text" v-model="form['student_college']" v-if="(form['student_information_id'] && $check_field('set','student_college')) || (!form['student_information_id'] && $check_field('add','student_college'))" :disabled="disabledObj['student_college_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','student_college')">
            {{ form['student_college'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','student_grade') || ($check_field('add','student_grade') || $check_field('set','student_grade'))" label="学生年级" name="student_grade">
                      <uni-easyinput type="text" v-model="form['student_grade']" v-if="(form['student_information_id'] && $check_field('set','student_grade')) || (!form['student_information_id'] && $check_field('add','student_grade'))" :disabled="disabledObj['student_grade_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','student_grade')">
            {{ form['student_grade'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','student_major') || ($check_field('add','student_major') || $check_field('set','student_major'))" label="学生专业" name="student_major">
                      <uni-easyinput type="text" v-model="form['student_major']" v-if="(form['student_information_id'] && $check_field('set','student_major')) || (!form['student_information_id'] && $check_field('add','student_major'))" :disabled="disabledObj['student_major_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','student_major')">
            {{ form['student_major'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','class_name') || ($check_field('add','class_name') || $check_field('set','class_name'))" label="班级名称" name="class_name">
                      <uni-easyinput type="text" v-model="form['class_name']" v-if="(form['student_information_id'] && $check_field('set','class_name')) || (!form['student_information_id'] && $check_field('add','class_name'))" :disabled="disabledObj['class_name_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','class_name')">
            {{ form['class_name'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','student_details') || ($check_field('add','student_details') || $check_field('set','student_details'))" label="学生详情" name="student_details">
              <uni-easyinput type="textarea" v-model="form['student_details']" v-if="(form['student_information_id'] && $check_field('set', 'student_details')) || (!form['student_information_id'] && $check_field('add', 'student_details'))" :disabled="disabledObj['student_details_isDisabled']"/>
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get', 'student_details')">
            {{ form['student_details'] }}
          </text>
            </uni-forms-item>
      </uni-forms>
      <view class="form-footer" v-if="$check_action('/student_information/view','set') || ($check_action('/student_information/view','add') || $check_option('/student_information/table','examine'))">
        <view class="me-btn btn-submit" @click="submit_()"> 提交 </view>
        <view class="me-btn btn-cancel" @click="cancel()">取消</view>
      </view>
      <view class="form_button" v-else>
        <view class="me-btn btn-cancel" @click="cancel()">返回</view>
      </view>
    </view>
  </view>
</template>

<script>
import mixin from '@/libs/mixins/page.js';
                                            import { uploadFilePathApi, uploadFileApi } from '@/api/common.js';
export default {
  mixins: [mixin],
  data() {
    return {
	  today: new Date().toISOString().split('T')[0],
      field: "student_information_id",
      url_add: "~/api/student_information/add?",
      url_set: "~/api/student_information/set?",
      url_get_obj: "~/api/student_information/get_obj?",
      url_upload: "~/api/student_information/upload?",
      student_name: null,
      student_account: null,
      student_gender: null,
      student_id: null,
      class_director: null,
      students_hometown: null,
      student_college: null,
      student_grade: null,
      student_major: null,
      class_name: null,
      student_details: null,
      query: {
        "student_information_id": 0,
      },
      form: {
          "student_name":  '', // 学生姓名
            "student_account": 0, // 学生账户
            "student_gender":  '', // 学生性别
            "student_id":  '', // 学生学号
            "class_director": 0, // 班级主任
            "students_hometown":  '', // 学生籍贯
            "student_college":  '', // 学生学院
            "student_grade":  '', // 学生年级
            "student_major":  '', // 学生专业
            "class_name":  '', // 班级名称
            "student_details":  '', // 学生详情
          "student_information_id": 0, // ID
      },
      disabledObj:{
          "student_name_isDisabled": false,
            "student_account_isDisabled": false,
            "student_gender_isDisabled": false,
            "student_id_isDisabled": false,
            "class_director_isDisabled": false,
            "students_hometown_isDisabled": false,
            "student_college_isDisabled": false,
            "student_grade_isDisabled": false,
            "student_major_isDisabled": false,
            "class_name_isDisabled": false,
            "student_details_isDisabled": false,
        },
                      // 用户列表
      list_user_student_account: [],
                              // 学生性别选项列表
      list_student_gender: [],
                              // 用户列表
      list_user_class_director: [],
                  // 用户组
      group_user_class_director: "",
                                                                };
  },
  methods: {
    /**
     * 提交前验证事件
     * @param {Object} 请求参数
     * @return {String} 验证成功返回null, 失败返回错误提示
     */
    async submit_check(param) {
                                                                                                                                                                                      return null;
    },
    changeLog(v, value) {
      this.form[value] = v;
    },
    uploadFile_(type) {
      // #ifdef APP-VUE
      uni.chooseFile({
        count: 1,
        success: (chooseImageRes) => {
          const tempFilePaths = chooseImageRes.tempFilePaths;
          this.successChoose(tempFilePaths[0], type);
        },
      });
      // #endif
      // #ifdef !APP-VUE
      if (
        uni.getSystemInfoSync().uniPlatform != 'mp-weixin' ||
        uni.getSystemInfoSync().platform == 'devtools'
      ) {
        uni.chooseImage({
          count: 1,
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFilePaths;
            this.successChoose(tempFilePaths[0], type);
          },
        });
      } else {
        wx.chooseMessageFile({
          count: 1,
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFiles;
            this.successChoose(tempFilePaths[0].path, type);
          },
        });
      }
      // #endif
    },
    successChoose(filePath, type) {
      uni.uploadFile({
        url: this.$fullUrl('~/api/student_information/upload?'), //仅为示例，非真实的接口地址
        filePath,
        name: 'file',
        success: (uploadFileRes) => {
          if (uploadFileRes.data.error) {
            uni.showToast({ title: uploadFileRes.data.error.message, icon: 'none' });
          } else {
            this[type] = JSON.parse(uploadFileRes.data).result.url;
          }
        },
      });
    },
    close_(type) {
      if (type == 'student_name') this['student_name'] = this.form['student_name'] = "";
      if (type == 'student_account') this['student_account'] = this.form['student_account'] = "";
      if (type == 'student_gender') this['student_gender'] = this.form['student_gender'] = "";
      if (type == 'student_id') this['student_id'] = this.form['student_id'] = "";
      if (type == 'class_director') this['class_director'] = this.form['class_director'] = "";
      if (type == 'students_hometown') this['students_hometown'] = this.form['students_hometown'] = "";
      if (type == 'student_college') this['student_college'] = this.form['student_college'] = "";
      if (type == 'student_grade') this['student_grade'] = this.form['student_grade'] = "";
      if (type == 'student_major') this['student_major'] = this.form['student_major'] = "";
      if (type == 'class_name') this['class_name'] = this.form['class_name'] = "";
      if (type == 'student_details') this['student_details'] = this.form['student_details'] = "";
    },
    submit_() {
      if (this['student_name'] !== null) this.form['student_name'] = this['student_name']
      if (this['student_account'] !== null) this.form['student_account'] = this['student_account']
      if (this['student_gender'] !== null) this.form['student_gender'] = this['student_gender']
      if (this['student_id'] !== null) this.form['student_id'] = this['student_id']
      if (this['class_director'] !== null) this.form['class_director'] = this['class_director']
      if (this['students_hometown'] !== null) this.form['students_hometown'] = this['students_hometown']
      if (this['student_college'] !== null) this.form['student_college'] = this['student_college']
      if (this['student_grade'] !== null) this.form['student_grade'] = this['student_grade']
      if (this['student_major'] !== null) this.form['student_major'] = this['student_major']
      if (this['class_name'] !== null) this.form['class_name'] = this['class_name']
      if (this['student_details'] !== null) this.form['student_details'] = this['student_details']
      console.log(this.form)
      this.submit();
    },
    /**
     * 上传文件
     * @param {Object} param文件参数
     */
    change_file(key_name) {
      var _self = this;
      // 选择图像方法
      uni.chooseFile({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (file) => {
          const tempFilePaths = file.tempFilePaths;
          const filePath = tempFilePaths[0];
          uploadFilePathApi(
            '/student_information/upload',
            filePath,
            undefined,
            {
              i_want_to_customize: 'test',
            },
            (task) => {
              task.onProgressUpdate((res) => {
                this.percent = res.progress;
                console.log('上传进度' + res.progress);
                console.log('已经上传的数据长度' + res.totalBytesSent);
                console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
              });
            }
          ).then((res) => {
            const filename = res.result.url;
            this.form[key_name] = filename;
          });
        },
        error: function (e) {
          console.log(e);
        },
      });
    },
    /**
     * 上传图片
     * @param {Object} param文件参数
     */
    change_img(key_name) {
      var _self = this;
      _self.upload_img_flag = false;
      // 选择图像方法
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (file) => {
          const tempFilePaths = file.tempFilePaths;
          const filePath = tempFilePaths[0];
          uploadFilePathApi('/student_information/upload',filePath,undefined,undefined,
            (task) => {
              task.onProgressUpdate((res) => {
                this.percent = res.progress;
                console.log('上传进度' + res.progress);
                console.log('已经上传的数据长度' + res.totalBytesSent);
                console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
              });
            }
          ).then((res) => {
            const filename = res.result.url;
            this.form[key_name] = filename;
          });
        },
        error: function (e) {
          console.log(e);
        },
      });
    },
    /**
     * 获取对象后获取缓存表单
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_before(param) {
      var form = uni.db.get('form');
      if (form) {
        delete form.examine_state;
        delete form.examine_reply;
        this.obj = uni.push(this.obj, form);
        this.form = uni.push(this.form, form);
      }
      var arr = [];
      for (let key in form) {
        arr.push(key);
      }
      for (var i = 0; i < arr.length; i++) {
        this.disabledObj[arr[i] + '_isDisabled'] = true;
      }
                                                  uni.db.del('form');

      return param;
    },
    
          /**
     * 获取学生用户用户列表
     */
    async get_list_user_student_account() {
      var json = await this.$get("~/api/user/get_list?user_group=学生用户");
      if(json.result && json.result.list){
        json.result.list.map((o) => this.list_user_student_account.push({value:o.user_id,text:o.nickname + '-' + o.username}));
      }
      else if(json.error){
        console.error(json.error);
      }
    },
      
        /**
     * 获取学生性别列表
     */
    async get_list_student_gender() {
          ['男','女'].map((o) => this.list_student_gender.push({value:o,text:o}));
            },
            
      
          /**
     * 获取教师用户用户列表
     */
    async get_list_user_class_director() {
      var json = await this.$get("~/api/user/get_list?user_group=教师用户");
      if(json.result && json.result.list){
        json.result.list.map((o) => this.list_user_class_director.push({value:o.user_id,text:o.nickname + '-' + o.username}));
      }
      else if(json.error){
        console.error(json.error);
      }
    },
        /**
     * 获取教师用户用户组
     */
    async get_group_user_class_director() {
      this.form["class_director"] = this.user.user_id;
      var json = await this.$get("~/api/user_group/get_obj?name=教师用户");
      if(json.result && json.result.obj){
        this.group_user_class_director = json.result.obj;
        this.get_user_session_class_director(this.form['class_director'])
      }
      else if(json.error){
        console.error(json.error);
      }
    },
    get_user_session_class_director(id){
      var _this = this;
      var user_id = {"user_id":_this.user.user_id}
      var url = "~/api/"+_this.group_user_class_director.source_table+"/get_obj"
      this.$get(url, user_id, function(res) {
        if (res.result && res.result.obj) {
          var arr = []
          for (let key in res.result.obj) {
            arr.push(key)
          }
          var arrForm = []
          for (let key in _this.form) {
            arrForm.push(key)
          }
          _this.form["class_director"] = _this.user.user_id
          _this.disabledObj['class_director' + '_isDisabled'] = true
          for (var i=0;i<arr.length;i++){
            if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
              for (var j = 0; j < arrForm.length; j++) {
                if (arr[i] === arrForm[j]) {
                  if (arr[i] !== "class_director") {
                    _this.form[arrForm[j]] = res.result.obj[arr[i]]
                    _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                    break;
                  } else {
                    _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                  }
                }
              }
            }
          }
        }
      });
    },
      
      
      
      
      
      
      
      /**
     * 获取对象之后
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_after(json, func) {
                                                                                                                                        },

    is_view() {
      var bl = this.user_group == '管理员';

      if (!bl) {
        bl = this.$check_action('/student_information/table', 'add');
        console.log(bl ? '你有表格添加权限视作有添加权限' : '你没有表格添加权限');
      }
      if (!bl) {
        bl = this.$check_action('/student_information/table', 'set');
        console.log(bl ? '你有表格添加权限视作有修改权限' : '你没有表格修改权限');
      }
      if (!bl) {
        bl = this.$check_action('/student_information/view', 'add');
        console.log(bl ? '你有视图添加权限视作有添加权限' : '你没有视图添加权限');
      }
      if (!bl) {
        bl = this.$check_action('/student_information/view', 'set');
        console.log(bl ? '你有视图修改权限视作有修改权限' : '你没有视图修改权限');
      }
      if (!bl) {
        bl = this.$check_action('/student_information/view', 'get');
        console.log(bl ? '你有视图查询权限视作有查询权限' : '你没有视图查询权限');
      }

      console.log(
        bl
          ? '具有当前页面的查看权，请注意这不代表你有字段的查看权'
          : '无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行'
      );

      return bl;
    },
  },
  created() {
            this.get_list_user_student_account();
              this.get_list_student_gender();
                this.get_list_user_class_director();
          this.get_group_user_class_director();
                                  },
};
</script>

<style lang="scss" scoped>
  @import '@/styles/pagesC/index.scss';
</style>